package view.conponent;

import com.alibaba.fastjson.JSON;
import model.assessment.Question;
import net.ToServer;
import net.passClass.PassData;
import net.passClass.PassQuestion;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

public class QuestionManager extends Box {
    final int WIDTH = 1080;

    private JTable table;
    private Vector<String> titles;
    private Vector<Vector<String>> tableData;
    private DefaultTableModel tableModel;

    public QuestionManager() {
        //指定垂直布局
        super(BoxLayout.Y_AXIS);

        //组装表格
        String[] ts = {"名称","类型","答案1","答案2","答案3","UID"};
        titles = new Vector<>();
        titles.addAll(Arrays.asList(ts));

        tableData = new Vector<>();

        tableModel = new DefaultTableModel(tableData, titles);
        table = new JTable(tableModel){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);

        JPanel textFieldPanel = new JPanel();
        textFieldPanel.setMaximumSize(new Dimension(WIDTH, 80));
        textFieldPanel.setMinimumSize(new Dimension(870, 80));
        textFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        Box box1 = Box.createVerticalBox();
        JTextField name = new JTextField(9);
        JLabel nameStr = new  JLabel("名称");
        box1.add(nameStr);
        box1.add(name);


        Box box2 = Box.createVerticalBox();
        JTextField type = new JTextField(9);
        JLabel typeStr = new  JLabel("类型");
        box2.add(typeStr);
        box2.add(type);


        Box box3 = Box.createVerticalBox();
        JTextField answers_0 = new JTextField(9);
        JLabel answers_0Str = new  JLabel("答案1");
        box3.add(answers_0Str);
        box3.add(answers_0);

        Box box4 = Box.createVerticalBox();
        JTextField answers_1 = new JTextField(9);
        JLabel answers_1Str = new  JLabel("答案2");
        box4.add(answers_1Str);
        box4.add(answers_1);


        Box box5 = Box.createVerticalBox();
        JTextField answers_2 = new JTextField(9);
        JLabel answers_2Str = new  JLabel("答案3");
        box5.add(answers_2Str);
        box5.add(answers_2);


        textFieldPanel.add(box1);
        textFieldPanel.add(box2);
        textFieldPanel.add(box3);
        textFieldPanel.add(box4);
        textFieldPanel.add(box5);
        this.add(textFieldPanel);

        JPanel btnPanel = new JPanel();
        btnPanel.setMaximumSize(new Dimension(WIDTH, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton addBtn = new JButton("添加");
        JButton deleteBtn = new JButton("删除");
        JButton updateBtn = new JButton("修改");
        JButton getBtn = new JButton("查找");
        JButton refreshBtn = new JButton("刷新");

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PassData receive = ToServer.receive("/assessment/question/add",
                        "name:"+name.getText()+"\n" +
                                "type:" +type.getText()+"\n" +
                                "answers_0:" +answers_0.getText()+"\n" +
                                "answers_1:" +answers_1.getText()+"\n" +
                                "answers_2:" +answers_2.getText());
                requestData();
            }
        });

        deleteBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                int[] selected = table.getSelectedRows();
                for (int num: selected) {
                    System.out.println(num);
                    ToServer.receive("/assessment/question/delete","uid:"+table.getValueAt(num,5));
                }
                requestData();
            }
        });

        updateBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {

                int selected = table.getSelectedRow();
                ToServer.receive("/assessment/question/update",
                        "name:"+name.getText()+"\n" +
                                "uid:"+table.getValueAt(selected,5)+"\n" +
                                "type:" +type.getText()+"\n" +
                                "answers_0:" +answers_0.getText()+"\n" +
                                "answers_1:" +answers_1.getText()+"\n" +
                                "answers_2:" +answers_2.getText());
                requestData();
            }
        });

        getBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                PassQuestion receive =(PassQuestion) ToServer.receive("/assessment/question/get",
                        "name:"+name.getText()+"\n" +
                                "type:" +type.getText()+"\n" +
                                "answers_0:" +answers_0.getText()+"\n" +
                                "answers_1:" +answers_1.getText()+"\n" +
                                "answers_2:" +answers_2.getText());
                requestData(receive.getData());
            }
        });

        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = table.getSelectedRow();
                if(row != -1) {
                    name.setText(table.getValueAt(table.getSelectedRow(),0).toString());
                    type.setText(table.getValueAt(table.getSelectedRow(),1).toString());
                    answers_0.setText(String.valueOf(table.getValueAt(table.getSelectedRow(),2)));
                    answers_1.setText(String.valueOf(table.getValueAt(table.getSelectedRow(),3)));
                    answers_2.setText(String.valueOf(table.getValueAt(table.getSelectedRow(),4)));
                }
            }
        });

        refreshBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                requestData();
            }
        });

        btnPanel.add(addBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(getBtn);
        btnPanel.add(refreshBtn);
        this.add(btnPanel);

        requestData();

    }

    public void requestData(){
        ArrayList<Question> data = ((PassQuestion)ToServer.receive("/assessment/question/get","\n")).getData();
        requestData(data);
    }

    public void requestData(ArrayList<Question> data){
        tableData.clear();
        for(int i = 0; i<data.size(); i++){
            Question qusetion = JSON.parseObject(JSON.toJSONString(data.get(i)),Question.class);
            Vector vector = new Vector();
            vector.add(qusetion.getName());
            vector.add(qusetion.getType());
            vector.add(qusetion.getAnswers_0());
            vector.add(qusetion.getAnswers_1());
            vector.add(qusetion.getAnswers_2());
            vector.add(qusetion.getUid());
            tableData.add(vector);
        }
        tableModel.fireTableDataChanged();

    }

}
